package com.macro.mall.common.filter;


import jakarta.servlet.*;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;


@Slf4j
public class ExceptionFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        try {
            chain.doFilter(request, response);
        } catch (Exception e) {
            log.error("接口错误："+e.getMessage(), e);
            // 异常捕获，发送到error controller
            request.setAttribute("filter_error", e);
            //将异常分发到/error/exThrow控制器
            request.getRequestDispatcher("/error/exThrow").forward(request, response);
        }
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void destroy() {

    }
}